package com.example.administrator.mvptest.view.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.administrator.mvptest.R;
import com.example.administrator.mvptest.view.databeans.ZhihuStory;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by Administrator on 2016-11-24.
 */
public class ZhihuStoryAdapter extends RecyclerView.Adapter {
    private Activity mActivity;
    private ArrayList<ZhihuStory>mStories;

    public  ZhihuStoryAdapter(Activity activity,ArrayList<ZhihuStory>stories)
    {
        mActivity=activity;
        mStories=stories;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view =LayoutInflater.from(mActivity).inflate(R.layout.zhihudaily_item,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        ViewHolder holder=(ViewHolder)viewHolder;
        holder.mNewsTitle.setText(mStories.get(position).getTitle());
        Glide.with(mActivity)
                .load(mStories.get(position).getImages().get(0))
                .placeholder(R.mipmap.ic_launcher)   //加载图片时等待的图片
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(holder.mNewImage);
    }

    @Override
    public int getItemCount() {
        return mStories.size();
    }

    static   class  ViewHolder extends RecyclerView.ViewHolder{
        //获取view对象
         @InjectView(R.id.news_image)
        ImageView mNewImage;

        @InjectView(R.id.news_title)
        TextView mNewsTitle;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.inject(this, itemView);
        }
    }
    public void addItem(ArrayList<ZhihuStory> stories) {
        int position = mStories.size();
        mStories.addAll(stories);
        notifyItemInserted(position);
    }
}
